package com.example.andylau96.res.Function.Account;

import android.app.Fragment;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.andylau96.res.Main.MainActivity;
import com.example.andylau96.res.R;
import com.example.andylau96.res.DataBase.UserDataBase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * Created by AndyLau96 on 2016/12/11.
 */
public class bAddFragment extends Fragment implements View.OnClickListener
{
    private Button ok;
    private ImageButton re;
    private EditText id,name,age,tel,pwd,per;
    private SQLiteDatabase db;
    private LinearLayout title;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.badd,container,false);
        ok= (Button) view.findViewById(R.id.badd_ok);
        id= (EditText) view.findViewById(R.id.badd_id);
        name= (EditText) view.findViewById(R.id.badd_name);
        age= (EditText) view.findViewById(R.id.badd_age);
        tel= (EditText) view.findViewById(R.id.badd_tel);
        title= (LinearLayout) view.findViewById(R.id.badd_title);
        re= (ImageButton) view.findViewById(R.id.badd_title_return);
        per= (EditText) view.findViewById(R.id.badd_per);
        pwd= (EditText) view.findViewById(R.id.badd_pwd);

        ok.setOnClickListener(this);
        re.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        int t=view.getId();
        if(t==R.id.badd_ok) {
            String mid = id.getText().toString();
            String mname = name.getText().toString();
            String mage = age.getText().toString();
            String mtel = tel.getText().toString();
            String mper=per.getText().toString();
            String mpwd=pwd.getText().toString();
            if (mname.length() == 0 || mname.length() == 0 || mtel.length() == 0||mper.length()==0||mpwd.length()==0) {
                Toast.makeText(getActivity(), "请输入完整信息！", Toast.LENGTH_SHORT).show();
            } else {
                db = new UserDataBase(getActivity().getApplicationContext()).getWritableDatabase();
                db.execSQL("insert into user values("+mid+",'"+mpwd+"',"+mper+")");
                db.execSQL("insert into worker values("+mid+",'"+mname+"',"+mage+",'"+mtel+"','"+"0-0-0"+"')");
                Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
                getFragmentManager().popBackStack();
            }
        }
        if(t==R.id.badd_title_return)
            getFragmentManager().popBackStack();

    }
    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }
}
